package com.rickpan.controller;

import com.rickpan.dto.request.CreateFolderRequest;
import com.rickpan.dto.request.MoveFolderRequest;
import com.rickpan.dto.request.RenameFolderRequest;
import com.rickpan.dto.response.FolderTreeNodeResponse;
import com.rickpan.dto.response.FolderPathResponse;
import com.rickpan.service.FolderService;
import com.rickpan.utils.ResponseUtils;
import com.rickpan.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文件夹管理控制器
 *
 * @author RickPan Team
 * @since 2025-07-02
 */
@RestController
@RequestMapping("/api/folders")
public class FolderController {

    @Autowired
    private FolderService folderService;

    /**
     * 获取文件夹树
     *
     * @param parentId 父文件夹ID，null表示根目录
     * @param depth 加载深度，默认3层
     * @return 文件夹树节点列表
     */
    @GetMapping("/tree")
    public ResponseEntity<?> getFolderTree(
            @RequestParam(required = false) Long parentId,
            @RequestParam(defaultValue = "3") Integer depth) {

        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            System.out.println("获取文件夹树: userId=" + currentUserId + ", parentId=" + parentId + ", depth=" + depth);

            List<FolderTreeNodeResponse> folderTree = folderService.getFolderTree(currentUserId, parentId, depth);

            return ResponseUtils.success(folderTree);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("获取文件夹树失败: " + e.getMessage());
        }
    }

    /**
     * 创建文件夹
     *
     * @param request 创建文件夹请求
     * @return 创建的文件夹信息
     */
    @PostMapping
    public ResponseEntity<?> createFolder(@RequestBody CreateFolderRequest request) {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            System.out.println("创建文件夹: userId=" + currentUserId + ", folderName=" + request.getName() + ", parentId=" + request.getParentId());

            FolderTreeNodeResponse folder = folderService.createFolder(currentUserId, request);

            return ResponseUtils.success(folder);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("创建文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 重命名文件夹
     *
     * @param folderId 文件夹ID
     * @param request 重命名请求
     * @return 更新后的文件夹信息
     */
    @PutMapping("/{folderId}/rename")
    public ResponseEntity<?> renameFolder(
            @PathVariable Long folderId,
            @RequestBody RenameFolderRequest request) {

        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            System.out.println("重命名文件夹: userId=" + currentUserId + ", folderId=" + folderId + ", newName=" + request.getName());

            FolderTreeNodeResponse folder = folderService.renameFolder(currentUserId, folderId, request);

            return ResponseUtils.success(folder);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("重命名文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 移动文件夹
     */
    @PutMapping("/{folderId}/move")
    public ResponseEntity<?> moveFolder(@PathVariable Long folderId, @RequestBody MoveFolderRequest request) {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            folderService.moveFolder(currentUserId, folderId, request);
            return ResponseUtils.success("文件夹移动成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("移动文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 删除文件夹
     */
    @DeleteMapping("/{folderId}")
    public ResponseEntity<?> deleteFolder(@PathVariable Long folderId) {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            folderService.deleteFolder(currentUserId, folderId);
            return ResponseUtils.success("文件夹删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("删除文件夹失败: " + e.getMessage());
        }
    }

    /**
     * 获取文件夹详情
     */
    @GetMapping("/{folderId}")
    public ResponseEntity<?> getFolderDetail(@PathVariable Long folderId) {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            FolderTreeNodeResponse folder = folderService.getFolderDetail(currentUserId, folderId);
            return ResponseUtils.success(folder);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("获取文件夹详情失败: " + e.getMessage());
        }
    }

    /**
     * 获取文件夹路径
     */
    @GetMapping("/{folderId}/path")
    public ResponseEntity<?> getFolderPath(@PathVariable Long folderId) {
        try {
            Long currentUserId = SecurityUtils.getCurrentUserId();
            FolderPathResponse folderPath = folderService.getFolderPath(currentUserId, folderId);
            return ResponseUtils.success(folderPath);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.error("获取文件夹路径失败: " + e.getMessage());
        }
    }
}
